﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEditor;

namespace Core
{
    internal class TexturePostprocessor : AssetPostprocessor
    {
        public void OnPreprocessTexture()
        {
            TextureImporter importer = this.assetImporter as TextureImporter;
            if (importer == null) 
                return;

            // 因为项目采用动态图集，所以ui下的图片需要开启可读模式，动态图集需求图片为RGBA32格式
            if (importer.assetPath.StartsWith($"Assets/{GameConst.UI_PATH}"))
            {
                importer.isReadable = true;

                SetDynamicAtlasSupport(importer, "Standalone");
                SetDynamicAtlasSupport(importer, "iPhone");
                SetDynamicAtlasSupport(importer, "Android");
                SetDynamicAtlasSupport(importer, "WebGL");
            }
        }

        private void SetDynamicAtlasSupport(TextureImporter importer, string platform)
        {
            var setting = importer.GetPlatformTextureSettings(platform);
            setting.overridden = true;
            setting.format = TextureImporterFormat.RGBA32;
            importer.SetPlatformTextureSettings(setting);
        }
    }
}
